local MapBase = require("app.Base.MapBase")

local SixWhiteMapLayer = class("SixWhiteMapLayer", MapBase)

function SixWhiteMapLayer:ctor(data)
    SixWhiteMapLayer.super.ctor(self,data)
    print("SixWhiteMapLayer")

    dump(self.data)
    self.mapPlane= { }

    self.gridToPos = {}    

    self.gridToData = {}

    self:initUI()
end

function SixWhiteMapLayer:onEnter()
    self:AddButtonClick()
end

function SixWhiteMapLayer:initUI()
    SixWhiteMapLayer.super.initUI(self)
    local data = self.data

    self.mapNode = display.newNode():addTo(self)
    self.mapNode:setScale(data.scale)
    self.mapNode:setScaleY(data.angle_y/90*data.scale)          
    self.mapNode:setSkewX(data.angle_x)

    for i = 1 , data.map_x + data.rand * 2 do
        self.gridToPos[i] = {}
        self.gridToData[i] = {}
        for t = 1 , data.map_y + data.rand * 2 do
--            local tipe = display.newSprite("UI/tipe/six.png"):addTo(self.mapNode)
            local tipe = td.CreateBtn({map={enabled = "UI/tipe/six.png",pressed = "UI/tipe/six.png",disabled = "UI/tipe/six.png"},text=""})
            tipe:addTo(self.mapNode)
            table.insert(self.mapPlane,tipe)

            local move_pos 
            if i%2 == 0 then 
                move_pos =cc.p(i*28,t*30)
            else
                move_pos =cc.p(i*28,t*30+15)
            end
            tipe:move(move_pos)
            if i<=data.rand or i > data.map_y + data.rand or t<=data.rand or t > data.map_y + data.rand then 
                data.terrain = td.Terrain.Unavailable
--                tipe:setColor(td.BTN_PRESSED_COLOR)
            else 
                data.terrain = math.random(4)--td.Terrain.Nothing
            end
            tipe:setColor(td.TerrainColor[data.terrain])

            tipe.grid = cc.p(i,t)
            tipe.data = data

            self.gridToPos[i][t]=move_pos
            self.gridToData[i][t] = {pos = move_pos,grid = cc.p(i,t),height = data.height,terrain = data.terrain}
        end
    end
end

function SixWhiteMapLayer:AddButtonClick()
    for i, tipe in ipairs(self.mapPlane) do
        
        td.BtnAddTouch(tipe,function ()
            self:onClickTipe(tipe.grid)            
        end)
    end   
end

function SixWhiteMapLayer:onClickTipe(grid,a)
    if a then grid = cc.p(grid,a) end    
    print("坐标 X:"..grid.x.." Y:"..grid.y)

    if self.clickTipeImage == nil then self.clickTipeImage = display.newSprite("UI/tipe/six_up.png") self.clickTipeImage:addTo(self.mapNode):setOpacity(100) end
    self.clickTipeImage:move(self.gridToPos[grid.x][grid.y])

    td.dispatchEvent(td.DispatchName.MAP_Click,{grid=grid})
end

function SixWhiteMapLayer:GetDataForGrid(grid)
    return self.gridToData[grid.x][grid.y]
end

function SixWhiteMapLayer:Updata(dt)
    for i, tipe in ipairs(self.mapPlane) do
        tipe:setColor(cc.c3b(math.random(255),math.random(255),math.random(255)))
        tipe:setOpacity(math.random(255))
    end
end

return SixWhiteMapLayer